/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author ASUS
 */
@WebServlet(urlPatterns = {"/enviarDB"})
public class enviarDB extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            int decide = Integer.parseInt(request.getParameter("eleccion"));


            //          out.println(id + "<br>");
//            out.println(nom + "<br>");


            if (decide == 0) {
                out.println("Usted no eleigio Acci&oacute;n por lo tanto no se realizo nada");
            } else if (decide == 1) {

                int id = Integer.parseInt(request.getParameter("numUno"));
                String nom = request.getParameter("numDos");

                Statement sta = ConexionSql.conectarse();
                String entra = "insert into norita values (" + id + ",'" + nom + "')";
                
                try {
                    if (sta.executeUpdate(entra) != sta.EXECUTE_FAILED) {
                        out.println("Entrada correcta");
                    } else {
                        out.println("Entrada incorrecta");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(enviarDB.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (decide == 2) {
                try {
                    Statement algo = ConexionSql.conectarse();
                    String consultar = "select * from norita";
                    ResultSet acep = algo.executeQuery(consultar);
                    while (acep.next()) {
                        out.println(acep.getInt("id") + "----");
                        out.println(acep.getString("Nombre") + "<br>");
                    }
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, "Hubo un error\ncon la consulta" + ex, "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else if (decide == 3) {
                int id2 = Integer.parseInt(request.getParameter("eliminar"));

                Statement sta2 = ConexionSql.conectarse();
                String entra = "delete from norita where id='" + id2 + "'";
                try {
                    if (sta2.executeUpdate(entra) != sta2.EXECUTE_FAILED) {
                        out.println("Eliminaci&oacute;n correcta");
                    } else {
                        out.println("Eliminaci&oacute;n incorrecta");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(enviarDB.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else if (decide == 4) {
                int id3 = Integer.parseInt(request.getParameter("actualiza"));
                String nom = request.getParameter("numDos");
                Statement sta3 = ConexionSql.conectarse();
                String entra = "UPDATE norita SET nombre='"+nom+"' where id='"+id3+"'";        
                try {
                    if (sta3.executeUpdate(entra) != sta3.EXECUTE_FAILED) {
                        out.println("Actualizaci&oacute; correcta");
                    } else {
                        out.println("Actualizaci&oacute; incorrecta");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(enviarDB.class.getName()).log(Level.SEVERE, null, ex);
                }
            }





        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
